﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace CoNatural.Data.VisualStudio.AddIn.Model {
	internal class Parameter {
		public Parameter(SqlParameter dbParameter) {
			DbParameter = dbParameter;
			DbParameter.Value = DBNull.Value;
		}

		public SqlParameter DbParameter { get; internal set; }
		public string Name { get { return DbParameter.ParameterName; } }
		public string Type {
			get {
				if (DbParameter.DbType == System.Data.DbType.String)
					return DbParameter.SqlDbType + "(" + (DbParameter.Size > 0 ? DbParameter.Size.ToString() : "MAX")  + ")";
				if (DbParameter.DbType == System.Data.DbType.Decimal)
					return DbParameter.SqlDbType + "(" + DbParameter.Precision + ", " + DbParameter.Scale + ")";
				if(DbParameter.DbType == DbType.Binary)
					return DbParameter.SqlDbType + "(" + (DbParameter.Size > 0 ? DbParameter.Size.ToString() : "MAX") + ")";
				return DbParameter.SqlDbType.ToString();
			}
		}
		public object Value {
			get {
				return DbParameter.Value;
			}
			set {
				try {
					if (value == null ||
						string.IsNullOrEmpty(value.ToString().Trim()) ||
						value.ToString().Trim().ToUpper() == "NULL")
						DbParameter.Value = DBNull.Value;
					else
						DbParameter.Value = value;
				}
				catch {
					DbParameter.Value = DBNull.Value;
				}
			}
		}
	}
}
